package br.com.fatec.cqr.view;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.JViewport;
import javax.swing.KeyStroke;

import br.com.fatec.cqr.model.EditorPaneCustomizado;

public class ManagementScreen {

	JPanel result = new JPanel();
	JTabbedPane query = new JTabbedPane();
	EditorPaneCustomizado display;
	JEditorPane displayNoSql;
	JFrame screen = new JFrame("CQR - Conversor de Query's Relacionais");
	Toolkit size = Toolkit.getDefaultToolkit();
	int height = size.getScreenSize().height;
	int width = size.getScreenSize().width;
	Color backgroundColor = new Color(119, 136, 153);
	ActionListener action = new Action();

	JPanel textPanel = new JPanel();
	JToolBar buttonPanel = new JToolBar();
	JMenuBar menu = new JMenuBar();

	public ManagementScreen() {
		URL url = this.getClass().getResource(
				"/br/com/fatec/cqr/view/resources/logo_aplicacao.png");
		Image icon = Toolkit.getDefaultToolkit().getImage(url);
		screen.setIconImage(icon);

		screen.setSize(width, height -= height * 0.05);
		preparaJanela();
		preparaMenuSuperior();
		preparaMenuInferior();
		painelTexto();
		painelResultado();
		preparaPaineis();

	}

	private void preparaJanela() {
		screen.getContentPane().setBackground(backgroundColor);
		screen.setDefaultCloseOperation(screen.EXIT_ON_CLOSE);
	}

	private void preparaPaineis() {

		GridLayout layoutTexto = new GridLayout(2, 1);
		textPanel.setLayout(layoutTexto);

		JPanel painelPrincipal = new JPanel();

		/*
		 * Gerenciadores de Layout dos paineis
		 */
		BorderLayout layout = new BorderLayout();
		painelPrincipal.setLayout(layout);
		GridLayout style = new GridLayout(1, 3);
		GridLayout gridLayout = new GridLayout(2, 1);
		BorderLayout border = new BorderLayout();
		/*
		 * Dimensao dos paineis
		 */
		buttonPanel.setPreferredSize(new Dimension(width, 25));
		textPanel.setPreferredSize(new Dimension(width - 40, height));

		/*
		 * Setando as cores de fundo de cada painel
		 */
		buttonPanel.setBackground(new Color(240, 255, 255));
		textPanel.setBackground(backgroundColor);

		painelPrincipal.add(buttonPanel, BorderLayout.NORTH);
		painelPrincipal.add(textPanel, BorderLayout.CENTER);

		screen.add(painelPrincipal);
		screen.setJMenuBar(menu);
		screen.setVisible(true);
	}

	private void preparaMenuSuperior() {

		menu.setBackground(new Color(240, 255, 255));
		JMenu menuFile = new JMenu("File");
		menuFile.setFont(new Font("Arial", Font.PLAIN, 12));
		menuFile.setBackground(new Color(240, 255, 255));
		menu.add(menuFile);

		JMenuItem subNew = new JMenuItem("New");
		subNew.setFont(new Font("Arial", Font.PLAIN, 12));
		subNew.setBackground(new Color(240, 255, 255));
		menuFile.add(subNew);
		subNew.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Action().newQuery(query, textPanel, result);
			}
		});

		JMenuItem subOpen = new JMenuItem("  Open");
		subOpen.setIcon(new ImageIcon(
				ManagementScreen.class
						.getResource("/br/com/fatec/cqr/view/resources/Open Project 16x16.png")));
		subOpen.setFont(new Font("Arial", Font.PLAIN, 12));
		subOpen.setBackground(new Color(240, 255, 255));
		menuFile.add(subOpen);
		subOpen.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Action().openFile(query, textPanel, result);
			}
		});

		JMenuItem subSave = new JMenuItem("  Save");
		subSave.setIcon(new ImageIcon(ManagementScreen.class
				.getResource("/br/com/fatec/cqr/view/resources/Save 16x16.png")));
		subSave.setFont(new Font("Arial", Font.PLAIN, 12));
		subSave.setBackground(new Color(240, 255, 255));
		menuFile.add(subSave);
		subSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Action().saveAction(displayNoSql, query);
			}
		});

		JMenuItem subExit = new JMenuItem(
				"Exit                                                  Alt+F4");
		subExit.setFont(new Font("Arial", Font.PLAIN, 12));
		subExit.setBackground(new Color(240, 255, 255));
		menuFile.add(subExit);
		subExit.addActionListener(action);

		JMenu menuEdit = new JMenu("Edit");
		menuEdit.setFont(new Font("Arial", Font.PLAIN, 12));
		menuEdit.setBackground(new Color(240, 255, 255));
		menu.add(menuEdit);

		JMenuItem subUndo = new JMenuItem("  Undo");
		subUndo.setIcon(new ImageIcon(ManagementScreen.class
				.getResource("/br/com/fatec/cqr/view/resources/Undo 16x16.png")));
		subUndo.setFont(new Font("Arial", Font.PLAIN, 12));
		subUndo.setBackground(new Color(240, 255, 255));
		menuEdit.add(subUndo);
		subUndo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				undo();
			}
		});

		JMenuItem subRedo = new JMenuItem("  Redo");
		subRedo.setIcon(new ImageIcon(ManagementScreen.class
				.getResource("/br/com/fatec/cqr/view/resources/Redo 16x16.png")));
		subRedo.setFont(new Font("Arial", Font.PLAIN, 12));
		subRedo.setBackground(new Color(240, 255, 255));
		menuEdit.add(subRedo);
		subRedo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				redo();
			}
		});

		JMenu menuHelp = new JMenu("Help");
		menuHelp.setFont(new Font("Arial", Font.PLAIN, 12));
		menuHelp.setBackground(new Color(240, 255, 255));
		menu.add(menuHelp);

		JMenuItem mntmViewHelp = new JMenuItem("  View Help");
		mntmViewHelp
				.setIcon(new ImageIcon(
						ManagementScreen.class
								.getResource("/br/com/fatec/cqr/view/resources/View Help 16x16.png")));
		mntmViewHelp.setFont(new Font("Arial", Font.PLAIN, 12));
		mntmViewHelp.setBackground(new Color(240, 255, 255));
		menuHelp.add(mntmViewHelp);
		mntmViewHelp.addActionListener(action);

		JMenuItem subAbout = new JMenuItem("About...");
		subAbout.setFont(new Font("Arial", Font.PLAIN, 12));
		subAbout.setBackground(new Color(240, 255, 255));
		menuHelp.add(subAbout);
		subAbout.addActionListener(action);
	}

	private void preparaMenuInferior() {

		String source = "resources/";
		JButton btnOpenFile = new JButton(" Open File");
		btnOpenFile.setToolTipText("Open File");
		btnOpenFile.setBackground(new Color(240, 255, 255));
		btnOpenFile.setIcon(new ImageIcon(ManagementScreen.class
				.getResource(source + "Open Project 16x16.png")));
		btnOpenFile.setPreferredSize(new Dimension(111, 21));
		btnOpenFile.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Action().openFile(query, textPanel, result);
			}
		});

		btnOpenFile.setFont(new Font("Arial", Font.ITALIC, 12));
		buttonPanel.add(btnOpenFile);

		JButton btnSave = new JButton();
		btnSave.setToolTipText("Save");
		btnSave.setBackground(new Color(240, 255, 255));
		btnSave.setIcon(new ImageIcon(ManagementScreen.class.getResource(source
				+ "Save 16x16.png")));
		btnSave.setPreferredSize(new Dimension(42, 21));
		btnSave.addActionListener(action);
		buttonPanel.add(btnSave);
		btnSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Action().saveAction(display, query);
			}
		});

		JButton btnNewQuery = new JButton(" New Query");
		btnNewQuery.setBackground(new Color(240, 255, 255));
		btnNewQuery.setToolTipText("New Query");
		btnNewQuery.setIcon(new ImageIcon(ManagementScreen.class
				.getResource(source + "New Query 16x16.png")));
		btnNewQuery.setPreferredSize(new Dimension(95, 21));
		btnNewQuery.addActionListener(action);
		buttonPanel.add(btnNewQuery);
		btnNewQuery.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Action().newQuery(query, textPanel, result);
			}
		});
		btnNewQuery.setFont(new Font("Arial", Font.ITALIC, 12));

		JButton btnUndo = new JButton();
		btnUndo.setBackground(new Color(240, 255, 255));
		btnUndo.setToolTipText("Undo");
		btnUndo.setIcon(new ImageIcon(ManagementScreen.class.getResource(source
				+ "Undo 16x16.png")));
		btnUndo.setPreferredSize(new Dimension(42, 21));
		btnUndo.addActionListener(action);
		buttonPanel.add(btnUndo);
		btnUndo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				undo();

			}
		});

		JButton btnRedo = new JButton();
		btnRedo.setBackground(new Color(240, 255, 255));
		btnRedo.setToolTipText("Redo");
		btnRedo.setIcon(new ImageIcon(ManagementScreen.class.getResource(source
				+ "Redo 16x16.png")));
		btnRedo.setPreferredSize(new Dimension(42, 21));
		btnRedo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				redo();
			}
		});
		buttonPanel.add(btnRedo);

		JButton btnTranslate = new JButton(" Translate");
		btnTranslate.setBackground(new Color(240, 255, 255));
		btnTranslate.setToolTipText("Translate");
		btnTranslate.setIcon(new ImageIcon(ManagementScreen.class
				.getResource(source + "Execute 16x16.png")));
		btnTranslate.setPreferredSize(new Dimension(110, 21));
		btnTranslate.addActionListener(action);
		buttonPanel.add(btnTranslate, BorderLayout.CENTER);
		btnTranslate.setFont(new Font("Arial", Font.ITALIC, 12));
		btnTranslate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

					new Action().runAction(query,display, displayNoSql);
			}
		}
		);

		JButton btnExport = new JButton(" Export");
		btnExport.setBackground(new Color(240, 255, 255));
		btnExport.setToolTipText("Export");
		btnExport.setBackground(new Color(240, 255, 255));
		btnExport.setIcon(new ImageIcon(ManagementScreen.class
				.getResource(source + "Export 16x16.png")));
		btnExport.setPreferredSize(new Dimension(110, 21));
		btnExport.setFont(new Font("Arial", Font.ITALIC, 12));
		btnExport.addActionListener(action);
		buttonPanel.add(btnExport);
		btnExport.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Action().export(displayNoSql);
			}
		});

		JButton btnClose = new JButton(" Close");
		btnClose.setBackground(new Color(240, 255, 255));
		btnClose.setToolTipText("Close");
		btnClose.setIcon(new ImageIcon(ManagementScreen.class
				.getResource(source + "close 16x16.png")));
		btnClose.setPreferredSize(new Dimension(110, 21));
		buttonPanel.add(btnClose, BorderLayout.CENTER);
		btnClose.setFont(new Font("Arial", Font.ITALIC, 12));
		btnClose.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new Action().close(query);
			}
		});

	}

	private void painelTexto() {

		query.setFont(new Font("Arial", Font.PLAIN, 12));

		display = new EditorPaneCustomizado();
		display.setFont(new Font("Arial", Font.BOLD, 14));
		display.setForeground(new Color(0, 0, 205));

		JScrollPane scroll = new JScrollPane(display);
		scroll.setVerticalScrollBarPolicy(scroll.VERTICAL_SCROLLBAR_ALWAYS);
		JScrollPane s2 = scroll;
		query.addTab("New Query", scroll);
		textPanel.add(query);
		new Action().close(query);
	}

	private void painelResultado() {

		BorderLayout border = new BorderLayout();
		result.setLayout(border);
		GridLayout grid = new GridLayout(1, 1);
		JPanel nosql = new JPanel();
		nosql.setLayout(grid);

		result.setFont(new Font("Arial", Font.PLAIN, 12));
		displayNoSql = new JEditorPane();

		Label lblnoSQL = new Label("MongoDB - NO-SQL");
		lblnoSQL.setFont(new Font("Arial", Font.PLAIN, 16));

		JScrollPane scrollnoSql = new JScrollPane(displayNoSql);
		scrollnoSql
				.setVerticalScrollBarPolicy(scrollnoSql.VERTICAL_SCROLLBAR_ALWAYS);
		displayNoSql.setEditable(false);

		// Adicionando os paineis e componentes em seus locais adequados
		nosql.add(scrollnoSql);
		result.add(nosql, BorderLayout.CENTER);
		result.add(lblnoSQL, BorderLayout.NORTH);
		textPanel.add(result);
	}

	public void undo() {
		Robot robot;
		try {

			int i = new Action().retornaAbaAtiva(query);
			if (i != -1) {
				JScrollPane scroll = (JScrollPane) query.getComponentAt(i);

				JViewport vp = scroll.getViewport();
				for (Component comp : vp.getComponents()) {
					if (comp instanceof JEditorPane) {
						JEditorPane jta = (JEditorPane) comp;
						robot = new Robot();
						robot.keyPress(KeyEvent.VK_CONTROL);
						robot.keyPress(KeyEvent.VK_Z);
						jta.requestFocus();
						robot.keyRelease(KeyEvent.VK_CONTROL);
						robot.keyRelease(KeyEvent.VK_Z);
					}
				}

			}
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void redo() {
		Robot robot;
		try {

			int i = new Action().retornaAbaAtiva(query);
			if (i != -1) {
				JScrollPane scroll = (JScrollPane) query.getComponentAt(i);

				JViewport vp = scroll.getViewport();
				for (Component comp : vp.getComponents()) {
					if (comp instanceof JEditorPane) {
						JEditorPane jta = (JEditorPane) comp;
						robot = new Robot();
						robot.keyPress(KeyEvent.VK_CONTROL);
						robot.keyPress(KeyEvent.VK_Y);
						jta.requestFocus();
						robot.keyRelease(KeyEvent.VK_CONTROL);
						robot.keyRelease(KeyEvent.VK_Y);
					}
				}
			}
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
